package from_0_to_50;

import java.util.HashMap;
import java.util.Map;

public class Q001_两数相加 {
    static int[] nums = {3,3,5};
    static int target = 6;
    static Map<Integer,Integer> map = new HashMap<>();
    public static void main(String[] args) {
        int[] result = new int[2];
        result = twoSum(nums,target);
        for (int i = 0; i < 3; i++) {
            System.out.println(result[i]);
        }
    }
    public static int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        for (int i = 0; i < nums.length; i++) {
            System.out.println(i);
            map.put(nums[i],i);
        }
        System.out.println(map);
        nums = quickSort(nums,0, nums.length-1);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i]+"\t");
        }
        System.out.println();
        for (int i = 0,j = nums.length-1; i < j; ) {
            if(nums[i] + nums[j] == target){
                result[0] = map.get(nums[i]);
                result[1] = map.get(nums[j]);
                break;
            }
            if(nums[i] + nums[j] < target)
                i++;
            else
                j--;

        }


        return result;
    }
//    快排
    public static int[] quickSort(int[] array,int start, int end){
        int pivot = array[start];
        int i = start;
        int j = end;
        while (i<j) {
            while (i<j && array[j] > pivot)
                j--;
            while (i<j && array[i] < pivot)
                i++;
            if (array[i]==array[j] && i<j) {
                i++;
            } else {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
        if (i-1>start)
            array=quickSort(array,start,i-1);
        if (j+1<end)
            array=quickSort(array,j+1,end);
        return array;
    }
}
